$(function () {

    //登录按钮绑定事件
    $('#submit').bind('click', function () {
        //获取公钥
        getPublicKey();
        var rsa = new RSAKey();
        rsa.setPublic($("#publicKey").val(), $("#exponent").val());
        var data = ajax(ctxPath + "login/login", "POST", {
            userName: rsa.encrypt($("#username").val()),
            password: rsa.encrypt($("#password").val()),
            captchaCode: $("#captchaCode").val()
        });

        if (200 != data.code) {
            alert(data.msg);
            return;
        }
        $(location).attr("href", ctxPath + "admin/demo/toTemplate");
    });


});

/**
 * 更换验证码图片
 * */
function updateImg() {
    $("#captcha_img").attr("src", ctxPath + "login/defaultKaptcha");
}


/**
 * 获取公钥
 * */
function getPublicKey() {
    var result = ajax(ctxPath + "login/getRSAKey", "GET", {});
    if (result.code != 200) {
        alert(result.msg);
        return;
    }
    $("#publicKey").val(result.data.modulus);
    $("#exponent").val(result.data.publicExponent);
    console.log("publicKey: "+result.data.modulus);
    console.log("exponent: "+result.data.publicExponent);
}
